
package netwrokproject.transporte;

import java.util.logging.Level;
import java.util.logging.Logger;

public class RTOTimer extends Thread {

    private long limite;
    private long inicio;
    Transporte listener;

    RTOTimer(long limite) {
        this.limite = limite;
    }

    @Override
    public void run() {
        this.comeca();
        long atual = System.currentTimeMillis();

        while( (atual - this.inicio) < limite ) {

            if(listener.AckAtual == listener.AckEsperado) {
                this.interrupt();
            }

            try {
                System.out.println("opa... timeout..... timeout..... timeout....");
                sleep(10000);
            } catch (InterruptedException ex) {
                Logger.getLogger(RTOTimer.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        if( !(listener.AckAtual == listener.AckEsperado) ) {
            // listener.timeoutEvent();
        }
    }

    public void comeca() {
        this.inicio = System.currentTimeMillis();
    }

    public void addListener(Transporte t) {
        listener = t;
    }
}
